package org.pjlib;

import android.app.Application;
import android.content.Context;
import android.graphics.Color;

import androidx.multidex.MultiDex;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import org.pjlib.net.HttpUtils;
import org.pjlib.ui.BaseActivity;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by ws on 2019/02/21.
 */
public class BaseApp extends Application {
    protected static Context ctx;

    public static Context getContext() {
        return ctx;
    }
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        MultiDex.install(this);
    }
    @Override
    public void onCreate() {
        super.onCreate();
        ctx = getApplicationContext();
        HttpUtils.getInstance().init(this);
    }

    /**
     * 记录所有打开的Activity，用于退出
     */
    private List<BaseActivity> activitys = null;

    // 添加Activity到容器中
    public void addActivity(BaseActivity activity) {
        if (activitys == null) {
            activitys = new LinkedList<BaseActivity>();
        }
        if (activitys != null && activitys.size() > 0) {
            if (!activitys.contains(activity)) {
                activitys.add(activity);
            }
        } else {
            activitys.add(activity);
        }
    }

    /**
     * 溢出Activity
     */
    public void removeActivity(BaseActivity activity) {
        if (activitys != null && activitys.size() > 0) {
            if (activitys.contains(activity)) {
                activitys.remove(activity);
            }
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }

    /**
     * 获取所有Activity
     *
     * @return
     */
    public List<BaseActivity> getActivitys() {
        return activitys;
    }

    // 遍历所有Activity并finish
    public void exit() {
        if (activitys != null && activitys.size() > 0) {
            for (BaseActivity activity : activitys) {
                if (activity != null) {
                    activity.finish();
                }
            }
        }
        HttpUtils.cacaelAll();
    }
}
